package com.jp.knowledge.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BusinessPlanActivity;
import com.jp.knowledge.activity.DocReadProgramActivity;
import com.jp.knowledge.activity.LoginActivity;
import com.jp.knowledge.activity.PackageListActivity;
import com.jp.knowledge.activity.PositionToolsActivity;
import com.jp.knowledge.activity.PositiontoolActivity;
import com.jp.knowledge.activity.ProductEvaluationActivity;
import com.jp.knowledge.activity.ProductInterfaceActivity;
import com.jp.knowledge.activity.PublicOfferingListActivity;
import com.jp.knowledge.activity.RecommendActivity;
import com.jp.knowledge.activity.ReportingProgramActivity;
import com.jp.knowledge.activity.SkillDetailActivity;
import com.jp.knowledge.activity.SolutionActivity;
import com.jp.knowledge.activity.TeamDocActivity;
import com.jp.knowledge.activity.VipCompanyZoneActivity;
import com.jp.knowledge.activity.VipZoneActivity;
import com.jp.knowledge.adapter.FoundAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.model.FoundAppData;
import com.jp.knowledge.model.FundAppHome;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.activity.OrganizeActivity;
import com.jp.knowledge.my.activity.UserVipActivityOld;
import com.jp.knowledge.my.activity.VipActivity;
import com.jp.knowledge.my.model.OrganizaModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 16/11/28.
 */

public class FoundGridItem extends LinearLayout {

    private TextView groupName;
    private JpRecycleView grid;
    private Context mContext;

    private View  mView;

    private FundAppHome homeAppData;
    private FoundAdapter adapter;


    private CommDialog commDialog;
    private JpApplication application;
    private UserData  userData;
    private   FoundAppData curAppData;
    private   int dialg_type;//1去登录  2 成为vip  3成为企业vip

    public FoundGridItem(Context context) {
        this(context,null);
    }

    public FoundGridItem(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FoundGridItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;

        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        application = (JpApplication) ((Activity)mContext).getApplication();
        commDialog = new CommDialog(mContext);
        commDialog.setLeftListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                commDialog.dismiss();
            }
        });
        commDialog.setRightListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialg_type==1){//登陆
                    mContext.startActivity(new Intent(mContext, LoginActivity.class));
                }else if(dialg_type==2){//成为vip
                    mContext.startActivity(new Intent(mContext, VipActivity.class));
                }else if(dialg_type==3){//vip企业
                    mContext.startActivity(new Intent(mContext, OrganizeActivity.class));
                }
            }
        });

        LinearLayout.LayoutParams  params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mView = LayoutInflater.from(mContext).inflate(R.layout.found_grid_item,null);
        addView(mView);
        mView.setLayoutParams(params);
        this.setOrientation(VERTICAL);
        this.setLayoutParams(params);

        groupName = (TextView) mView.findViewById(R.id.group_name);
        grid = (JpRecycleView) mView.findViewById(R.id.recle_view);


        initRecycleview( null);
    }

    /**
     * 设置或者更新首页app数据
     * @param homeAppData
     */
    public void setHomeData(FundAppHome homeAppData){
        this.homeAppData = homeAppData;
        groupName.setText(homeAppData.getGroupName());
        
        adapter.updataAdapter(homeAppData.getGropuApps());
    }

    /**
     * 初始化recyceView
     */
    private void initRecycleview(List< FoundAppData> list) {
        grid.setHasFixedSize(true);
        grid.setNestedScrollingEnabled(false);
        GridLayoutManager gridManager = new GridLayoutManager(mContext, 4);
        grid.setLayoutManager(gridManager);
        grid.addItemDecoration(new JpDiver(mContext));
        adapter = new FoundAdapter(mContext,list);
        grid.setAdapter(adapter);

        adapter.setOnItemListener(new OnItemListerner() {
            @Override
            public void onItemClick(View view, int position) {
                FoundAppData  appData = homeAppData.getGropuApps().get(position);
                curAppData = appData;

//                double permission = appData.getPermission();
//                if(appData.getActivation() == 1 || permission == 0){    // 无需登陆
//                    toNext(appData);
//                } else if(!application.isLogin()){  // 登陆
//                    dialg_type=1;
//                    commDialog.setRight("登录");
//                    commDialog.setContent("请登录后才能使用此功能");
//                    commDialog.show();
//                } else if(permission==2){//指定权限
//                    double viewPermit = appData.getViewPermit();
//                    commDialog.setContent(appData.getViewPrompt());
//                    UserData userData = application.getUserData();
//
//                    if(viewPermit==0) {
//                        toNext(appData);
//                    } else if(viewPermit==1){
//                        if(userData != null && userData.getIsVip() == 1) {
//                            toNext(appData);
//                        } else {
//                            dialg_type=2;
//                            commDialog.setRight("成为VIP会员");
//                            commDialog.show();
//                        }
//                    }else{
//                        List<OrganizaModel> companyList = userData.getCompanyList();
//                        if(companyList != null && companyList.size() > 0)
//                            for(OrganizaModel organizaModel : companyList)
//                                if(organizaModel.getCompanyVip() == 1){
//                                    toNext(appData);
//                                    return;
//                                }
//                        dialg_type=3;
//                        commDialog.setRight("成为VIP企业");
//                        commDialog.show();
//                    }
//                }

                double permission = appData.getPermission();
                double viewPermit = appData.getViewPermit();
                commDialog.setContent(appData.getViewPrompt());
                commDialog.showCancleBtn();
                if(appData.getActivation() == 1){
                    toNext(appData);
                } else if(permission == 1){
                    dialg_type=1;
                    commDialog.setRight("登录");
                    commDialog.show();
                } else if(permission == 2){
                    if(viewPermit == 1){
                        dialg_type=2;
                        commDialog.setRight("成为VIP会员");
                    } else if(viewPermit == 4){
                        dialg_type=3;
                        commDialog.setRight("成为VIP企业");
                    } else{
                        dialg_type=4;
                        commDialog.hideCancleBtn();
                        commDialog.setRight("确定");
                    }
                    commDialog.show();
                }
            }
        });
    }

    private void toNext(FoundAppData appData) {
        /**
         * discover/reportList	行业报告
         appCode	discover/planList	行业方案
         appCode	discover/kengPoList	产品报告
         appCode	discover/documentList	办公文档
         */
        String name = appData.getName();
        String code = appData.getCode();
        if("discover/kengPoList".equals(code)) {                // 	产品报告
            mContext.startActivity(new Intent(mContext, ProductEvaluationActivity.class).putExtra("name",name));
        } else if("discover/documentList".equals(code)) {         // 办公文档
            mContext.startActivity(new Intent(mContext, ReportingProgramActivity.class).putExtra("name",name).putExtra("action",18));
        } else if("discover/reportList".equals(code)) {         // 行业报告
            mContext.startActivity(new Intent(mContext, ReportingProgramActivity.class).putExtra("name",name));
        } else if("discover/planList".equals(code)) {           //行业方案
            mContext.startActivity(new Intent(mContext, SolutionActivity.class).putExtra("name",name));
        }  else if("discover/recommendedList".equals(code)) {    // 全民推荐
            mContext.startActivity(new Intent(mContext, RecommendActivity.class).putExtra("name",name));
        } else if("discover/galleryList".equals(code)) {        // App界面
            mContext.startActivity(new Intent(mContext, ProductInterfaceActivity.class).putExtra("name",name).putExtra("action",1));
        } else if("discover/webUiList".equals(code)) {        // 网站界面
            mContext.startActivity(new Intent(mContext, ProductInterfaceActivity.class)
                    .putExtra("name",name)
                    .putExtra("action",2)
                    .putExtra("isIcon", true));
        } else if("discover/publicityUiList".equals(code)) {        // 产品宣传图
            mContext.startActivity(new Intent(mContext, ProductInterfaceActivity.class).putExtra("name",name).putExtra("action",3));
        } else if("discover/posterUiList".equals(code)) {        // 手机海报
            mContext.startActivity(new Intent(mContext, ProductInterfaceActivity.class).putExtra("name",name).putExtra("action",4));
        } else if("discover/jobToolsList".equals(code)) {        // 岗位工具
//            mContext.startActivity(new Intent(mContext, PositiontoolActivity.class));
            mContext.startActivity(new Intent(mContext, PositionToolsActivity.class));
        }else if("discover/productList".equals(code)){//文档模板
            mContext.startActivity(new Intent(mContext, DocReadProgramActivity.class).putExtra("name",name));
        }else if("discover/vipUserExclusive".equals(code)){//VIP会员区
            mContext.startActivity(new Intent(mContext, VipZoneActivity.class));
        } else if("discover/vipCompanyExclusive".equals(code)){//VIP企业区
            mContext.startActivity(new Intent(mContext, VipCompanyZoneActivity.class));
        } else if("discover/teamFile".equals(code)){// 团队文档
            mContext.startActivity(new Intent(mContext, TeamDocActivity.class));
        } else if("discover/businessPlanList".equals(code)){// 商业计划书
            mContext.startActivity(new Intent(mContext, BusinessPlanActivity.class).putExtra("name",name));
        } else if("discover/publicOfferingList".equals(code)){//上市公开书
            mContext.startActivity(new Intent(mContext, PublicOfferingListActivity.class).putExtra("name",name));
        } else if("discover/packageList".equals(code)){//知识专辑
            mContext.startActivity(new Intent(mContext, PackageListActivity.class).putExtra("name",name));
        }else if(!TextUtils.isEmpty(code)){
//            JpApplicationWebActivity.gotoWebActivity(mContext, code);
            ToasUtil.toast(mContext,"此类型太神秘，程序员不知道跳到哪里,code="+code);
        }
    }
}
